package io.renren.modules.business.controller;

import java.util.Map;
import io.renren.modules.business.entity.EProjectScheduleEntity;
import io.renren.modules.business.entity.EUserProjectRelEntity;
import io.renren.modules.business.service.EUserProjectRelService;
import io.renren.modules.sys.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.renren.modules.business.service.EProjectScheduleService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 项目进度表
 */
@RestController
@RequestMapping("/business/eprojectschedule")
public class EProjectScheduleController extends AbstractController {

    @Autowired
    private EProjectScheduleService eProjectScheduleService;

    @Autowired
    private EUserProjectRelService eUserProjectRelService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = eProjectScheduleService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		EProjectScheduleEntity eProjectSchedule = eProjectScheduleService.getById(id);

        return R.ok().put("eProjectSchedule", eProjectSchedule);
    }

    /**
     * 项目进度列表查询
     * @param pageNum
     * @param pageSize
     * @param projectId
     * @param chargePersonId
     * @param flag
     * @param isFinish
     * @return
     */
    @GetMapping("/query")
    public R queryProjectScheduleList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                      @RequestParam(value = "projectId", required = false) Long projectId,
                                      @RequestParam(value = "chargePersonId", required = false) Long chargePersonId,
                                      @RequestParam(value = "flag", defaultValue = "1") String flag,
                                      @RequestParam(value = "isFinish", required = false) String isFinish) {
        if (getUserAuthority() != 1) {
            return R.ok().put("page", eProjectScheduleService.queryProjectScheduleList(pageNum, pageSize, projectId, getUserId(), flag, isFinish));
        }
        return R.ok().put("page", eProjectScheduleService.queryProjectScheduleList(pageNum, pageSize, projectId, chargePersonId, flag, isFinish));
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody EProjectScheduleEntity eProjectSchedule){
		eProjectScheduleService.save(eProjectSchedule);
        EUserProjectRelEntity eUserProjectRelEntity = new EUserProjectRelEntity();
        eUserProjectRelEntity.setProjectId(eProjectSchedule.getProjectId());
        eUserProjectRelEntity.setUserId(eProjectSchedule.getChargePersonId());
        eUserProjectRelService.save(eUserProjectRelEntity);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody EProjectScheduleEntity eProjectSchedule){
		eProjectScheduleService.updateById(eProjectSchedule);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestParam("id") Long id){
		eProjectScheduleService.removeById(id);

        return R.ok();
    }

}
